package cn.e4j.modular.crm.controller;

import cn.digiwin.wx.utils.WeixinHandler;
import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.ToolUtil;
import cn.e4j.modular.api.service.FwpgService;
import cn.e4j.modular.crm.entity.MmraT;
import cn.e4j.modular.crm.entity.MmrdT;
import cn.e4j.modular.crm.service.impl.MmrdTServiceImpl;
import cn.e4j.modular.crm.service.impl.MmrfTServiceImpl;
import cn.e4j.modular.crm.service.impl.MmrnTServiceImpl;
import cn.e4j.modular.utils.CrmExceptionEnum;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 服务预约维护
 * @author miaoyl
 * @since 2019-04-03
 */
@Controller
@RequestMapping("/crm/gwai740")
public class Gwai740Controller extends BaseController {
    private Logger logger = LoggerFactory.getLogger(Gwai740Controller.class);

    @Autowired
    private MmrdTServiceImpl mmrdTService;

    @Autowired
    private MmrfTServiceImpl mmrfTService;

    @Autowired
    private MmrnTServiceImpl mmrnTService;

    @Autowired
    private FwpgService fwpgService;

    /**
     * 跳转服务预约维护管理页面
     */
    @RequiresPermissions("crm:gwai740:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwai740.html";
    }

    /**
     * 查询服务预约维护信息列表
     */
    @RequestMapping("/getMmrdList")
    @RequiresPermissions("crm:gwai740:init")
    @ResponseBody
    public ResponseData getMmrdList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = mmrdTService.getMmrdList(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 新增
     */
    @RequestMapping("/add")
    @RequiresPermissions("crm:gwai740:init")
    @ResponseBody
    public ResponseData add(MmrdT mmrdT, @DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        mmrdT.setMMRDENT(shiroUser.getDBC());
        mmrdT.setMMRDDOCNO("WXFW" + DateUtils.formatDate(new Date(), "yyyyMMddHHmmssSSS"));
        mmrdT.setMMRDSEQ(new BigDecimal(1));
        inDto.put("ENT",shiroUser.getDBC());
        inDto.put("MMRDDOCNO",mmrdT.getMMRDDOCNO());
        inDto.put("MMRDSEQ",mmrdT.getMMRDSEQ());
        // 判断是否存在
        //List<MmrdT> mmrdTList = this.mmrdTService.getData(mmrdT);
        List<Dto> mmrdTList = this.mmrdTService.getData2(inDto);
        if (mmrdTList.size() > 0) {
            return new ErrorResponseData(CrmExceptionEnum.EXISTED_THE_PARAMS.getCode(),
                    CrmExceptionEnum.EXISTED_THE_PARAMS.getMessage());
        }
        Dto gwak = this.mmrdTService.selectGwak23_25(inDto);
        if(gwak != null){
            mmrdT.setMMRD006(gwak.getString("GWAK023"));
            mmrdT.setMMRD007(gwak.getString("GWAK024"));
            mmrdT.setMMRD008(gwak.getString("GWAK025"));
            if (gwak.getString("GWAK025") != null) {
                mmrdT.setMMRD009(gwak.getString("GWAK025"));
            } else if (gwak.getString("GWAK024") != null) {
                mmrdT.setMMRD009(gwak.getString("GWAK024"));
            } else if (gwak.getString("GWAK023") != null) {
                mmrdT.setMMRD009(gwak.getString("GWAK023"));
            } else if (gwak.getString("GWAK022") != null) {
                mmrdT.setMMRD009(gwak.getString("GWAK022"));
            }

            //客户已购设备信息表-mmra_t
            QueryWrapper<MmraT> wrapper = new QueryWrapper<>();
            wrapper.eq("MMRAENT",mmrdT.getMMRDENT());
            wrapper.eq("MMRA001",inDto.getString("GWAK001"));
            wrapper.eq("MMRA002",inDto.getString("MMRD002"));
            wrapper.eq("MMRA003",inDto.getString("MMRD003"));
            MmraT mmraT = new MmraT().selectOne(wrapper);

            if(mmraT!=null && ToolUtil.isNotEmpty(mmraT.getMMRA021())){
                mmrdT.setMMRD017(mmraT.getMMRA021());
            }

        }
        this.mmrdTService.add(mmrdT);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("crm:gwai740:init")
    @ResponseBody
    public ResponseData edit(MmrdT mmrdT, @DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        JSONObject jsonObject = inDto.getReqJsonObject();
        Integer code = null;
        if("paigong".equals(inDto.getString("operation"))){
            String mmrfdocno = "WXPG" + DateUtils.formatDate(new Date(), "yyyyMMddHHmmssSSS");
            inDto.put("MMRFDOCNO",mmrfdocno);
            inDto.put("MMRFSEQ",1);
            Integer i = this.mmrfTService.getMmrfCount(inDto);
            if(i > 0){
                return new ErrorResponseData(CrmExceptionEnum.EXISTED_THE_PARAMS.getCode(),
                        CrmExceptionEnum.EXISTED_THE_PARAMS.getMessage());
            }
        }
        if (jsonObject != null && ("huifang").equals(jsonObject.getString("operation"))){
            inDto.put("operation","huifang");
            mmrdT = JSONObject.parseArray(jsonObject.getString("list"),MmrdT.class).get(0);
            String mmrndocno = "WXPG" + DateUtils.formatDate(new Date(), "yyyyMMddHHmmssSSS");
            inDto.put("MMRNDOCNO",mmrndocno);
            Integer i = this.mmrnTService.getMmrnCount(inDto);
            if(i > 0) {
                return new ErrorResponseData(CrmExceptionEnum.EXISTED_THE_PARAMS.getCode(),
                        CrmExceptionEnum.EXISTED_THE_PARAMS.getMessage());
            }
        }
        code = this.mmrdTService.edit(mmrdT, inDto);

        if(code!=null && code==200 && "paigong".equals(inDto.getString("operation"))){
            String goodname = inDto.getString("MMRA004");

            if (ToolUtil.isNotEmpty(goodname)){
                String mmrf007 = inDto.getString("MMRF007");
                if (ToolUtil.isNotEmpty(mmrf007)){
                    String yyuser = inDto.getString("MMRD017");
                    //派工人员
                    Dto pgDto = Dtos.newDto();
                    pgDto.put("userid", mmrf007);
                    pgDto.put("GWAAENT", inDto.getString("ENT"));
                    Dto gwaa2 = fwpgService.getGwaa2(pgDto);

                    if (ToolUtil.isEmpty(gwaa2))
                        logger.error("消息中派工人员获取为空！");
                    else{
                        //给派工人员发送消息
                        String pgmsg = String.format("设备[%s]已安排您去维修，请注意！",goodname);
                        WeixinHandler.sendMsg(gwaa2,pgmsg);

                        String pguser = gwaa2.getString("NAME");

                        if (ToolUtil.isNotEmpty(yyuser)){
                            //给预约人员发送消息
                            Dto yyDto = new Dto();
                            yyDto.put("GWAA001",yyuser);
                            yyDto.put("GWAAENT", inDto.getString("ENT"));
                            Dto gwaa21 = fwpgService.getGwaa2(yyDto);

                            if (ToolUtil.isNotEmpty(gwaa21) && pguser!=null){
                                //预约者消息
                                String msg = String.format("设备[%s]已经安排[%s]工程师去服务！",goodname,pguser);
                                WeixinHandler.sendMsg(gwaa21,msg);
                            }else
                                logger.error("消息中预约人员为空！");

                        }else
                            logger.error("消息中预约人员为空！");
                    }
                }else
                    logger.error("消息中派工人员获取为空！");
            }else
                logger.error("消息中品名为空,消息无法发送！");


        }

        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("crm:gwai740:init")
    @ResponseBody
    public ResponseData del(MmrdT mmrdT) {
        this.mmrdTService.delete(mmrdT);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/delDataBatch")
    @RequiresPermissions("crm:gwai740:init")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<MmrdT> mmrdTList = inDto.getReqList(MmrdT.class);
        this.mmrdTService.delDataBatch(mmrdTList);
        return SUCCESS_TIP;
    }

    @RequestMapping("/getMmrd013List")
    @RequiresPermissions("crm:gwai740:init")
    @ResponseBody
    public ResponseData getMmrd013List(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> list  = mmrdTService.getMmrd013List(inDto);
        return ResponseData.success(list);
    }

    @RequestMapping("/getMmraList")
    @ResponseBody
    public ResponseData getMmraList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = mmrdTService.getMmraList(page, inDto);
        return new PageReData(page, result);
    }

    @RequestMapping("/getMmrddocnoPage")
    @ResponseBody
    public ResponseData getMmrddocnoPage(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = mmrdTService.getMmrddocnoPage(page, inDto);
        return new PageReData(page, result);
    }

    @RequestMapping("/getUsers")
    @RequiresPermissions("crm:gwai740:init")
    @ResponseBody
    public ResponseData getUsers(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> list  = mmrdTService.getUsers(inDto);
        return ResponseData.success(list);
    }

    @RequestMapping("/getStarList")
    @RequiresPermissions("crm:gwai740:init")
    @ResponseBody
    public ResponseData getStarList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> list  = mmrdTService.getStarList(inDto);
        return ResponseData.success(list);
    }

    @RequestMapping("/getMMRF")
    @ResponseBody
    public ResponseData getMMRF(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto outDto  = mmrdTService.getMMRF(inDto);
        return ResponseData.success(outDto);
    }

    @RequestMapping("/getMMRN")
    @ResponseBody
    public ResponseData getMMRN(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> list  = mmrdTService.getMMRN(inDto);
        return ResponseData.success(list);
    }

    @RequestMapping("/getMmrddocno")
    @ResponseBody
    public ResponseData getMmrddocno(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = mmrdTService.getMmrddocno(page, inDto);
        return new PageReData(page, result);
    }

    @RequestMapping("/getMmrq001")
    @ResponseBody
    public ResponseData getMmrq001(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Dto> outDto  = mmrdTService.getMmrq001(inDto);
        return ResponseData.success(outDto);
    }
}
